﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.UI;
using Ext.Net;
using Ext.Net.Utilities;
using KamLib.Controls;
using Component = Ext.Net.Component;
using VBoxLayout = Ext.Net.VBoxLayout;

namespace KamLib
{
    public class MainPagePanel : Panel
    {
        public PageNode PageNode { get; set; }

        [DefaultValue("10px 30px 20px 30px")]
        public override string PaddingSummary
        {
            get { return ViewState.Try("PaddingSummary", "10px 30px 20px 30px"); }
            set { base.PaddingSummary = value; }
        }
    }


    public static class MainPagePanelEquiper
    {
        public static void Apply(UserControl uc, PageNode p)
        {
            var controls = new List<Control>(uc.Controls.OfType<Control>());
            uc.Controls.Clear();


            var pheader = controls.OfType<PageHeader>().FirstOrDefault();
            if (pheader == null)
                pheader = new PageHeader();
            else
                controls.Remove(pheader);

            if (pheader.Height.IsEmpty) pheader.Height = 110;

            if (p != null)
            {
                if (pheader.Title.IsEmpty()) pheader.Title = p.Title;
                if (pheader.IconCls.IsEmpty()) pheader.IconCls = "icon-" + p.Icon + "-64s";
                if (pheader.Description.IsEmpty()) pheader.Description = p.Desc;
            }

            var monitor = controls.OfType<MonitorControl>().FirstOrDefault();
            if (monitor != null && pheader.Items.OfType<MonitorControl>().FirstOrDefault() == null)
            {
                pheader.Items.Add(monitor);
                if (pheader.Height.Value < 120) pheader.Height = 120;
                if (monitor.Margins.IsEmpty()) monitor.Margins = "-28px 0 0 20px";
                controls.Remove(monitor);
            }

            BoxItem box2;
            var vbox = new VBoxLayout
            {
                Align = VBoxAlign.Stretch,
                BoxItems =
                {
                    new BoxItem { Items = { pheader } },
                    (box2 = new BoxItem { Flex = 1 }),
                }
            };


            box2.Items.AddRange(controls.OfType<Component>());
            uc.Controls.Add(vbox);

            controls.RemoveAll(a=> a is Component);
            foreach (var control in controls)
            {
                uc.Controls.Add(control);
            }
            
        }
    }
}
